package org.meeting.radish.controller;

import lombok.extern.slf4j.Slf4j;
import org.meeting.radish.entity.TestCase;
import org.meeting.radish.service.TestCaseService;
import org.meeting.radish.util.TestCaseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/test/case")
public class TestCaseController {

    @Resource
    TestCaseService testCaseService;

    @GetMapping("/delete/{tid}")
    public String deleteTestCase(@PathVariable("tid") Integer tid) {
        Integer qid = testCaseService.queryQidByTid(tid);
        testCaseService.deleteTestCase(tid);
        return "redirect:/questions/update/" + qid;
    }

    @PostMapping("/upload")
    public String uploadTestCase(MultipartFile[] files, Integer qid) throws IOException {

        Map<String, TestCase> testcases = TestCaseUtil.getTestCasesFromFiles(files, qid);

        for (String key : testcases.keySet()) {
            testCaseService.addTestCase(testcases.get(key));
        }

        return "redirect:/questions/update/" + qid;
    }

}
